package com.bingo.controller.iterceptor;


import com.bingo.entity.User;
import com.bingo.service.UserService;
import com.bingo.until.CookieUtil;
import com.bingo.until.HostHolder;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

@Component
@Slf4j
public class LoginTicketInterceptor implements HandlerInterceptor {


    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    private RedisTemplate redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    private HostHolder hostHolder;
    @Autowired
    public void setHostHolder(HostHolder hostHolder) {
        this.hostHolder = hostHolder;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求路径,如果包含backend则校验用户是否为管理员
        String path = request.getRequestURI();

        // 从cookie中获取凭证
        String token = CookieUtil.getValue(request, "token");
        if (token != null) {
            //根据凭证从redis中获取对应用户
            User user = (User) redisTemplate.opsForValue().get("token:" + token);
            if (user != null) {
                //即使登录，普通用户也不可以进入后台
                //是上报用户但访问的不是上报接口
                hostHolder.setUser(user);
            } else {
                //未登录
                //如果请求后端
                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                HashMap<String, String> map = new HashMap<>();
                map.put("msg", "登录状态已过期，请重新登录");
                map.put("code", "201");
                ObjectMapper mapper = new ObjectMapper();
                String json = mapper.writeValueAsString(map);
                response.getWriter().write(json);
                return false;
            }
        } else {
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            HashMap<String, String> map = new HashMap<>();
            map.put("msg", "请先登录");
            map.put("code", "201");
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(map);
            response.getWriter().write(json);
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        hostHolder.clear();
    }
}
